//表盘类型
var type = "sbsy";
$(document).ready(function(){  
    $('.form-inline-block Wdate').bind('focus',function(){WdatePicker()});  
    });
//日期
var beginDate;
var endDate;
var curTime = 'hour';
var granu = 'hour';
$(function() {
	/**********************表盘***********************/
	//首包时延表盘
	sbsyChar = echarts.init(document.getElementById('sbsy'));
	//首页打开时延表盘
	sydksyChar = echarts.init(document.getElementById('sydksy'));
	//DNS成功率表盘
	dnscglChar = echarts.init(document.getElementById('dnscgl'));
	//视频下载速率表盘
	spxzslChar = echarts.init(document.getElementById('spxzsl'));
	//IM成功率表盘
	imcglChar = echarts.init(document.getElementById('imcgl'));
	/**********************END OF 表盘***********************/
	
	/**********************指标图***********************/
	//指标变化趋势指标图
	 zbbhqsDiagram = echarts.init(document.getElementById('zbbhqsDiagram'));
	//质差SP指标图
	 //zcspDiagram = echarts.init(document.getElementById('zcspDiagram'));
	//质差站点指标图
	// zczdDiagram = echarts.init(document.getElementById('zczdDiagram'));
	//用户质差分布指标图
	// yhzcfbDiagram = echarts.init(document.getElementById('yhzcfbDiagram'));
	//访问质差分布指标图
	// fwzcfbDiagram = echarts.init(document.getElementById('fwzcfbDiagram'));
	/**********************END OF 指标图***********************/
    showArearList("city_code");
	$("#sbsytt").attr("checked",true);
	
	$("input[name='tt']").change(function(){
		type=$(this).val();
		initDataElement(type);
	});
	
	var timeSize = $('input[name="timeSize"]');
	timeSize.click(function() {
		granu = $(this).val();
		var granuDivId1 = granu + 'ComId1';
		var granuDivId2 = granu + 'ComId2';
		var curTimeDivId1 = curTime + 'ComId1';
		var curTimeDivId2 = curTime + 'ComId2';
		if (curTime == granu)
			return;
		$('#' + curTimeDivId1).hide();
		$('#' + curTimeDivId2).hide();
		$('#' + granuDivId1).show();
		$('#' + granuDivId2).show();
		curTime = granu;	
	});	
	getList();
//	zbbhqsDiagram.on('click',function(param){
//		zbbhqsDiagramLinkTo(param.seriesName,param.name);
//	});
//	zcspDiagram.on('click',function(param){
//		zcspDiagramLinkTo(param.name);
//	});
//	zczdDiagram.on('click',function(param){
//		zczdDiagramLinkTo(param.name);
//	});
//	fwzcfbDiagram.on('click',function(param){
//		fwzcfbDiagramLinkTo(param.name);
//	});
	
})
function getStartTime() {		
	return $('#' + granu + 'beginDate').val();		
}
function getEndTime() {
	return $('#' + granu + 'endDate').val();
}
function zbbhqsDiagramLinkTo(seriesName,eCharsTime) {
	if(seriesName == '请求次数'){
		return;
	}
	var timeSize=$('input:radio[name="timeSize"]:checked').val();
	var beginDate=getStartTime();
	var endDate=getEndTime();
	var jspType = "";
	var url = "/businessAnalysis/hcq/index?";
	var title = "HTTP过程指标"
	if(type == 'sbsy'){//首包时延
		jspType = "kqiEvalution_sbsy";
	}
	if(type == 'sydksy'){//首页打开时延
		jspType = "kqiEvalution_sydksy";
	}
	if(type == 'dnscgl'){//DNS成功率
		jspType = "kqiEvalution_dnscgl";
		url = "/businessAnalysis/dns/index?";
		title = "DNS指标";
	}
	if(type == 'spxzsl'){//视频下载速率
		jspType = "kqiEvalution_spxzsl";
	}
	if(type == 'imcgl'){//IM成功率
		jspType = "kqiEvalution_imcgl";
	}
	var p ="jspType="+jspType+"&eCharsTime="+eCharsTime+"&timeSize="+timeSize+"&beginDate="+beginDate+"&endDate="+endDate+"&seriesName="+seriesName;
	url += p;
	
	linkTab(title, url)
}
/*
function fwzcfbDiagramLinkTo(city_code) {
	var timeSize=$('input:radio[name="timeSize"]:checked').val();
	var beginDate=$("#beginDate").val();
	var endDate=$("#endDate").val();
	var p ="jspType=kqiEvalution_fwzcfb&city_code="+city_code+"&timeSize="+timeSize+"&queryTime="+beginDate+"&endDate="+endDate;
	var url = "/businessSense/kQIAnalysis/index?" + p;
	var title = "KQI分析"
	linkTab(title, url)
}

function zczdDiagramLinkTo(name) {
	var timeSize=$('input:radio[name="timeSize"]:checked').val();
	var beginDate=$("#beginDate").val();
	var endDate=$("#endDate").val();
	var p ="jspType=kqiEvalution_zczd&name="+name+"&timeSize="+timeSize+"&beginDate="+beginDate+"&endDate="+endDate;
	var url = "/businessSense/spAnalysis/index?" + p;
	var title = "SP分析"
	linkTab(title, url)
}

function zcspDiagramLinkTo(name) {
	var timeSize=$('input:radio[name="timeSize"]:checked').val();
	var beginDate=$("#beginDate").val();
	var endDate=$("#endDate").val();
	var p ="jspType=kqiEvalution_zcsp&name="+name+"&timeSize="+timeSize+"&beginDate="+beginDate+"&endDate="+endDate;
	var url = "/businessSense/spAnalysis/index?" + p;
	var title = "SP分析"
	linkTab(title, url)
}
*/
/**
 * 初始化各项数据元素
 * @param type
 * 		表盘类型（sbsy:首包时延表盘；sydksy:首页打开时延表盘；dnscgl:DNS成功率表盘；spxzsl:视频下载速率表盘；imcgl:IM成功率表盘；）
 */
function initDataElement(type) {
	//初始化指标变化趋势指标图
	initZbbhqsDiagram(type);
	//初始化质差SP指标图
	//initZcspDiagram(type);
	//初始化质差站点指标图
	//initZczdDiagram(type);
	//初始化用户质差分布指标图
	//initYhzcfbDiagram(type);
	//初始化访问质差分布指标图
	//initFwzcfbDiagram(type);
	//初始化质差终端排行表格
	//initZczdphTable(type);
	//初始化质差APN排行表格
	//initZcapnphTable(type);
	//初始化质差小区表格
	//initZcxqTable(type);
	//初始化质差用户表格
	//initZcyhTable(type);
}

function getList(){
	var timeSize=$('input:radio[name="timeSize"]:checked').val();
	if(timeSize==undefined){
		alert("请选择时间粒度！");
		return;
	}
	
	beginDate=getStartTime();
	endDate=getEndTime();
	var startSenconds = new Date(beginDate.replace("-", "/").replace("-", "/")).getTime();
	var endSenconds = new Date(endDate.replace("-", "/").replace("-", "/")).getTime();
	if(endSenconds <= startSenconds) {
		alert('结束时间必须大于开始时间！');
		return ;
	}
	//解决火狐和个别IE版本不兼容new Date(string)
	/*
	var checkDate = beginDate;
	if(timeSize=="day"){
		checkDate = endDate;
		var time = new Date(checkDate.replace("-", "/").replace("-", "/"));
		time.setDate(time.getDate() + 0);
		var dayEndDay=new XDate(time).toString('yyyy-MM-dd')+(" 23:59:59");
		endDate = dayEndDay;
	}else if(timeSize=="month"){
		var firstDate = new Date(checkDate.replace("-", "/").replace("-", "/"));
		firstDate.setDate(1); //第一天
		var monthFirstDay=new XDate(firstDate).toString('yyyy-MM-dd')+" 00:00:00";
		var tmpDate = new Date(firstDate);
		tmpDate.setMonth(firstDate.getMonth()+1);
		tmpDate.setDate(0);
		var monthEndDay=new XDate(tmpDate).toString('yyyy-MM-dd')+(" 23:59:59");
		beginDate = monthFirstDay;
		endDate = monthEndDay;
	}
	*/
    initSbsyChar();
	initSydksyChar();
	initDnscglChar();
	initSpxzslChar();
	initImcglChar();
	initDataElement(type);
}

function resetQueryForm() {
	$('#queryForm')[0].reset();
}